package com.project.internship.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Bases;
import com.project.internship.entity.Teacher;
import com.project.internship.entity.User;
import com.project.internship.mapper.BasesMapper;
import com.project.internship.mapper.TeacherMapper;
import com.project.internship.mapper.UserMapper;
import com.project.internship.service.TeacherService;
import com.project.internship.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.internship.utils.UserUtils;
import com.project.internship.utils.YearUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-04-15
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    BasesMapper basesMapper;
    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(username);
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        user.setRoles(userMapper.getUserRolesById(user.getRoleId()));
        return user;
    }

    @Override
    public IPage<User> getBaseUsers(Page<User> page, String keyword) {
        return userMapper.getBaseUsers(page, UserUtils.getCurrentUser().getDepartmentId(),keyword);
    }

    @Override
    public IPage<User> getTeacherUsers(Page<User> page, String keyword) {
        return userMapper.getTeacherUsers(page, UserUtils.getCurrentUser().getDepartmentId(),keyword);
    }

    @Override
    public List<User> getAllotUser() {
        if(UserUtils.getCurrentUser().getRoleId() == 3) {
            return userMapper.getAllotBaseUser(UserUtils.getCurrentUser().getTeacherId(), YearUtils.getCurrentYear());
        }
        if(UserUtils.getCurrentUser().getRoleId() == 4) {
            return userMapper.getAllotTeacherUser(UserUtils.getCurrentUser().getBaseId(),YearUtils.getCurrentYear());
        }
        return null;
    }

    @Override
    public List<Bases> getNonBasesName() {
        return userMapper.getNonBasesName(UserUtils.getCurrentUser().getDepartmentId());
    }

    @Override
    public List<Teacher> getNonTeacherName() {
        return userMapper.getNonTeacherName(UserUtils.getCurrentUser().getDepartmentId());
    }

    @Override
    public Integer addBaseUser(User user) {

        user.setDepartmentId(UserUtils.getCurrentUser().getDepartmentId());

        Integer id = basesMapper.getBaseIdByName(user.getName());
        user.setBaseId(id);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //将明文密码pwd 加盐处理存入 password
        user.setPassword(encoder.encode(user.getPwd()));

        user.setRoleId(4);
//        user.setEnabled(true);

        return userMapper.insert(user);
    }

    @Override
    public Integer addTeacherUser(User user) {

        user.setDepartmentId(UserUtils.getCurrentUser().getDepartmentId());

        int id = Integer.parseInt(user.getName());
        String name = teacherMapper.getTeacherNameById(id);
        user.setName(name);
        user.setTeacherId(id);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //将明文密码pwd 加盐处理存入 password
        user.setPassword(encoder.encode(user.getPwd()));

        user.setRoleId(3);
        user.setEnabled(true);

        return userMapper.insert(user);
    }

    @Override
    public Integer updateUser(User user) {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        //将明文密码pwd 加盐处理存入 password
        user.setPassword(encoder.encode(user.getPwd()));

        return userMapper.updateById(user);
    }

    @Override
    public Integer deleteUser(Integer id) {
        return userMapper.deleteById(id);
    }

}
